﻿using System;
using System.Collections.Generic;
using System.Runtime.Caching;
using System.Text;
using System.Threading.Tasks;

namespace Qianke.Core.Common
{
    public class CacheHelper
    {
        public static MemoryCache _cache = MemoryCache.Default;

        public static void Set<TModel>(string keyName, TModel model)
        {

            _cache.Set(keyName, model, DateTime.MaxValue);
        }

        public static TModel Get<TModel>(string keyName)
            where TModel : class
        {
            return _cache.Get(keyName) as TModel;
        }

        /// <summary>
        /// 设置缓存 绝对过期时间
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <param name="keyName"></param>
        /// <param name="model"></param>
        /// <param name="lastTime">过期时间</param>
        public static void Set<TModel>(string keyName, TModel model, DateTime lastTime)
        {
            _cache.Set(keyName, model, lastTime);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <param name="keyName"></param>
        /// <param name="model"></param>
        /// <param name="ticks">相对时间 秒数</param>
        public static void Set<TModel>(string keyName, TModel model, int seconds)
        {
            var policy = new CacheItemPolicy
            {
                SlidingExpiration = TimeSpan.FromSeconds(seconds)
            };
            _cache.Set(keyName, model, policy);
        }

        /// <summary>
        /// 设置缓存失效
        /// </summary>
        /// <param name="keyName"></param>
        public static void ClearCache(string keyName)
        {
            _cache.Remove(keyName);
        }

        /// <summary>
        /// 获取缓存
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="key">关键key</param>
        /// <param name="fnGetData"></param>
        /// <param name="ticks">相对毫秒数</param>
        /// <returns></returns>
        public static async Task<TModel> GetOrAddAsync<TModel>(string keyName, Func<Task<TModel>> fnGetData, int tSeconds) where TModel : class
        {
            var data = Get<TModel>(keyName);
            if (data == null)
            {
                data = await fnGetData();
                if (data != null)
                    Set(keyName, data, tSeconds);
            }
            return data;
        }

        public static async Task<TModel> GetOrAddAsync<TModel>(string keyName, Func<Task<TModel>> fnGetData,DateTime outTime) where TModel : class
        {
            var data = Get<TModel>(keyName);
            if (data == null)
            {
                data = await fnGetData();
                if (data != null)
                    Set(keyName, data, outTime);
            }
            return data;
        }

        /// <summary>
        /// 获取缓存
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="key">关键key</param>
        /// <param name="fnGetData"></param>
        /// <param name="ticks">相对毫秒数</param>
        /// <returns></returns>
        public static Task<TModel> Get<TModel>(long key, Func<Task<TModel>> fnGetData, int tSeconds) where TModel : class
        {
            return GetOrAddAsync($"{typeof(TModel)}_{key}", fnGetData, tSeconds);
        }
    }
}
